#include <bits/stdc++.h>
using namespace std;

using i16 = short;
using i32 = int;
using i64 = long long;
using i128 = __int128_t;
using u16 = unsigned short;
using u32 = unsigned;
using u64 = unsigned long long;
using u128 = __uint128_t;
using f32 = float;
using f64 = double;
using f80 = long double;
#define i32 i64
const f80 MY_LOVE = 1.226e99L;
const i64 mod = 1226999999;
const i32 INF = 0x3f3f3f3f;
#define mpr make_pair
#define fir(p) ((p).first)
#define sec(p) ((p).second)
#define brkpr(p) fir(p),sec(p)
#define prferr(str,...) fprintf(stderr,str,##__VA_ARGS__)
// #define DLOCAL
template<class T> inline T& chmin(T& a,T b) { return a=(a<b?a:b); }
template<class T> inline T& chmax(T& a,T b) { return a=(a>b?a:b); }

const i32 N = 200005;

i32 T,n,a[N];

namespace P50 {
	// current forever blue
	i32 dp[2005][2005];
	// dp[i][j]: cur i, last red j or last blue j-2000
	inline void solve() {
		memset(dp,0,sizeof(dp));
		for(i32 i=1; i<=n; ++i) {
			i32 x=(a[i]==a[i-1])?a[i]:0;
			for(i32 j=0; j<i-1; ++j) {
				chmax(dp[i][j],dp[i-1][j]+x);
				chmax(dp[i][i-1],dp[i-1][j]+(a[i]==a[j]?a[i]:0));
			}
			// for(i32 j=0; j<=i-1; ++j) prferr("%lld ",dp[i][j]);
			// prferr("\n");
		}
		i32 ans=0;
		for(i32 i=0; i<=n-1; ++i) chmax(ans,dp[n][i]);
		printf("%lld\n",ans);
	}
}

namespace P100 {
	bool exi[N];
	i32 tot,mx[N],cnt[N];
	inline void add(i32 p,i32 x) {
		for(; p<=1000000; p+=p&-p) mx[p]+=x;
	}
	inline i32 qry(i32 p) {
		if(p<0) return 0;
		i32 ret=0;
		for(; p; p-=p&-p) ret+=mx[p];
		return ret;
	}
	inline i32 qry(i32 L,i32 R) {
		if(L>R) return 0;
		return qry(R)-qry(L-1);
	}
	struct Seg {
		struct node {
			i32 l,r,mx,tg;
			inline void up(node L,node R) { mx=max(L.mx,R.mx); }
			inline void down(node P) { mx+=P.tg,tg+=P.tg; }
		} t[N<<4];
		inline void clear() { memset(t,0,sizeof(t)); }
		inline void up(i32 p) { t[p].up(t[p<<1],t[p<<1|1]); }
		inline void build(i32 p,i32 l,i32 r) {
			t[p].l=l,t[p].r=r;
			if(l==r) return t[p].mx=0,void();
			i32 mid=(l+r)>>1;
			build(p<<1,l,mid);
			build(p<<1|1,mid+1,r);
			up(p);
		}
		inline void down(i32 p) {
			t[p<<1].down(t[p]);
			t[p<<1|1].down(t[p]);
			t[p].tg=0;
		}
		inline void add(i32 p,i32 l,i32 r,i32 L,i32 R,i32 x) {
			if(L>R) return;
			if(L<=l&&r<=R) return t[p].mx+=x,t[p].tg+=x,void();
			down(p);
			i32 mid=(l+r)>>1;
			if(L<=mid) add(p<<1,l,mid,L,R,x);
			if(mid<R) add(p<<1|1,mid+1,r,L,R,x);
			up(p);
		}
		inline i32 qry(i32 p,i32 l,i32 r,i32 L,i32 R) {
			if(L>R) return 0;
			if(L<=l&&r<=R) return t[p].mx;
			down(p);
			i32 mid=(l+r)>>1,ret=0;
			if(L<=mid) chmax(ret,qry(p<<1,l,mid,L,R));
			if(mid<R) chmax(ret,qry(p<<1|1,mid+1,r,L,R));
			return ret;
		}
	} seg;
	inline void solve() {
		tot=0;
		memset(cnt,0,sizeof(cnt));
		memset(exi,0,sizeof(exi));
		memset(mx,0,sizeof(mx));
		seg.clear();
		seg.build(1,0,n);
		for(i32 i=1; i<=n; ++i) {
			i32 x=(a[i]==a[i-1]?a[i]:0);
			i32 y=seg.qry(1,0,n,0,i-2);
			if(exi[a[i]]) chmax(y,qry(cnt[a[i]],cnt[a[i]])+a[i]);
			seg.add(1,0,n,0,i-2,x);
			seg.add(1,0,n,i-1,i-1,y);
			add(1,x); add(tot+1,-x);
			if(i>1&&!exi[a[i-1]]) cnt[a[i-1]]=++tot,exi[a[i-1]]=1;
			if(i>1) {
				i32 pos=cnt[a[i-1]];
				i32 cur=qry(pos,pos);
				// if(i==7) {
					// prferr("pos(%lld) cur(%lld) y(%lld)\n",pos,cur,y);
					// prferr("%lld %lld\n",qry(pos,pos),qry(pos+1,pos+1));
					// prferr("%lld %lld\n",y-cur,cur-y);
				// }
				if(cur<y) {
					add(pos,y-cur);
					// prferr("%lld %lld\n",qry(pos),qry(pos+1));
					add(pos+1,cur-y);
					// prferr("%lld %lld\n",qry(pos),qry(pos+1));
				}
			}
			// prferr("seg:");
			// for(i32 i=1; i<=n; ++i) prferr(" %lld",seg.qry(1,0,n,i,i));
			// prferr("\n");
			// prferr("mx:");
			// for(i32 i=1; i<=tot; ++i) prferr(" %lld",qry(i,i));
			// prferr("\n");
			/*
			add(i-1,i-1,max(qry(0,i-2),mx(a[i])+a[i]))
			add(0,i-2,x)
			*/
		}
		printf("%lld\n",seg.qry(1,0,n,0,n));
	}
}

inline void solve() {
	scanf("%lld",&n);
	for(i32 i=1; i<=n; ++i) scanf("%lld",a+i);
	if(n<=2000) P50::solve();
	// else P100::solve();
	// P100::solve();
}

signed main() {
	// #if !defined(DLOCAL) && !defined(DLOCALI)
	freopen("color.in","r",stdin);
	// #endif
	// #if !defined(DLOCAL) && !defined(DLOCALO)
	freopen("color.out","w",stdout);
	// #endif
	scanf("%lld",&T);
	while(T--) solve();
}
